<?php 	
include "header.php";
include "koneksi.php";
include "auth.php";
CheckMainAdmin();
?>


<!-- main -->
<div id="main">
<?php include 'sidebar.php';?>

<?php
if (isset($_POST["add"])=="add_data")
{
	$jdl = $_POST["judul"];
	
	if ($jdl == ''){
		echo"<SCRIPT> alert('Isikan kolom Username !!'); window.history.go(-1); </SCRIPT>\n";

	}
	echo '<div id="text">';
	echo '<div class="title">';
	echo '<h1>Tanbah Data Materi</h1>';
	echo '</div>';	
	echo $jdl;
	if (($_FILES["file"]["error"] > 0) && $_FILES["file"]["type"] != "application/x-shockwave-flash")// jika file kosong
	{
	  echo "File Kosong";
	}
	else
	{
	  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
	  echo "Type: " . $_FILES["file"]["type"] . "<br />";
	  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
	  echo "Stored in: " . $_FILES["file"]["tmp_name"];
	  $fupload_name = $_FILES["file"]["name"];
	  $vdir_upload = "../flash/";
	  $vfile_upload = $vdir_upload.$fupload_name;

		//Simpan gambar dalam ukuran sebenarnya
		move_uploaded_file($_FILES["file"]["tmp_name"],$vfile_upload);
		
		mysql_query("INSERT INTO tb_materi(`judul`,`desc`,`path`,`menu`)VALUES(
		'".$_POST['judul']."',
		'".$_POST['desc']."',
		'".$fupload_name."',
		'".$_POST['menu']."'
		)") or die (mysql_error());
		echo "<meta http-equiv=\"refresh\" content=\"0;URL=olah_materi.php?act=list\">";
		exit();
	}
	echo '</div>';
	
	//echo "<meta http-equiv=\"refresh\" content=\"0;URL=gangguan.php\">";
	//exit();
}
elseif (isset($_POST["edit"])=="edit_data")
{
	$jdl = $_POST["judul"];
	$id = $_POST["id"];
	$path = $_POST["path"];
	echo '<div id="text">';
	echo '<div class="title">';
	echo '<h1>Tanbah Data Materi</h1>';
	echo '</div>';	
	echo $jdl;
	if (($_FILES["file"]["error"] > 0) && $_FILES["file"]["type"] != "application/x-shockwave-flash")// jika file kosong
	{
	  echo "File Kosong";
	}
	else
	{
	  unlink('../flash/'.$path);
	  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
	  echo "Type: " . $_FILES["file"]["type"] . "<br />";
	  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
	  echo "Stored in: " . $_FILES["file"]["tmp_name"];
	  $fupload_name = $_FILES["file"]["name"];
	  $vdir_upload = "../flash/";
	  $vfile_upload = $vdir_upload.$fupload_name;

		//Simpan gambar dalam ukuran sebenarnya
		move_uploaded_file($_FILES["file"]["tmp_name"],$vfile_upload);
		
		mysql_query("UPDATE tb_materi SET
		`judul` = '".$_POST['judul']."',
		`desc` = '".$_POST['desc']."',
		`path` = '".$fupload_name."',
		`menu` = '".$_POST['menu']."'
		WHERE `id_materi` = '$id'") or die (mysql_error());
		echo "<meta http-equiv=\"refresh\" content=\"0;URL=olah_materi.php?act=list\">";
		exit();
	}
	echo '</div>';
	
	//echo "<meta http-equiv=\"refresh\" content=\"0;URL=gangguan.php\">";
	//exit();
}

?>


<?php if ($_GET['act'] == 'list')
{

	$sql = mysql_query("SELECT * FROM tb_materi");
	echo '<div id="text">';
		echo '<div class="title">';
		echo '<h1>Olah Materi</h1>';
		echo '</div>';	
		echo '<a class="add" href="olah_materi.php?act=add">Tambah<a>';
		echo '<br />';
		echo '<table border="0" cellpadding="0" cellspacing="0">';
			echo '<tr class="zebra">';
				echo '<th>No</th>';
				echo '<th width="100">Judul</th>';
				echo '<th width="200">Menu</th>';
				echo '<th width="100">File</th>';
				echo '<th>Aksi</th>';
			echo '</tr>';
			
			$i = 1;
			while ($row = mysql_fetch_array($sql))
			{
				if ($i %2 == 0){
				echo '<tr class="zebra">';
				}
				else{
				echo '<tr class="zebra2">';
				}
					echo '<td>'.$i.'</td>';
					echo '<td>'.$row["judul"].'</td>';
					echo '<td>'.$row["menu"].'</td>';
					echo '<td>'.$row["path"].'</td>';
					echo '<td><a href="olah_materi.php?act=edit&id='.$row["id_materi"].'">Edit</a> | <a href="olah_materi.php?act=delete&id='.$row["id_materi"].'">Hapus</a></td>';
					
				echo '</tr>';
			$i++;
			}
		echo'</table>';
	echo '</div>';
}

elseif ($_GET['act'] == 'add')
{
	echo '<div id="text">';
		echo '<div class="title">';
		echo '<h1>Tambah Data Materi</h1>';
		echo '</div>';    
		echo '<form  method="post" action="olah_materi.php" enctype="multipart/form-data">';
		echo '<input name="add" type="hidden" value="add_data" />';
		echo '<p>';
			echo '<label>Judul</label>';
			echo '<input type="text" class="form_field" name="judul" size="50" value="" />';
		echo '</p>';
		echo '<p>';
			echo '<label>Deskripsi</label>';
			echo '<textarea class="form_field_area" name="desc" size="30" value=""></textarea>';
		echo '</p>';
		echo '<p>';
			echo '<label>Menu</label>';
			echo '<input type="text" class="form_field" name="menu" size="50" value="" />';
		echo '</p>';
		echo '<p>';
			echo '<label>File (.swf) </label>';
			echo '<input type="file" name="file" class="subtitle">';
		echo '</p>';
		echo '<br /> <br />';

		echo '<div id="btn">';
		echo '<input type="submit" class="submit" name="edit" value="Simpan">';
		echo '<input class="submit" name="Batal" type="reset" value="Back" onclick="window.history.go(-1)"/>';
		echo '</form>';
		echo '</div>';
	echo '</div>';
}

elseif ($_GET['act'] == 'edit')
{
	
	$idnya = $_GET['id'];
	$sql = mysql_query("SELECT * FROM tb_materi WHERE id_materi = $idnya");	
	$data=mysql_fetch_array($sql);
	
	echo '<div id="text">';
		echo '<div class="title">';
		echo '<h1>Edit Data Materi</h1>';
		echo '</div>';    
		echo '<form  method="post" action="olah_materi.php" enctype="multipart/form-data">';
		echo '<input name="edit" type="hidden" value="edit_data" />';
		echo '<p>';
			echo '<label>Judul</label>';
			echo '<input type="text" class="form_field" name="judul" size="50" value="'.$data['judul'].'" />';
		echo '</p>';
		echo '<p>';
			echo '<label>Deskripsi</label>';
			echo '<textarea class="form_field_area" name="desc" size="30" value="">'.$data['desc'].'</textarea>';
		echo '</p>';
		echo '<p>';
			echo '<label>Menu</label>';
			echo '<input type="text" class="form_field" name="menu" size="50" value="'.$data['menu'].'" />';
		echo '</p>';
		echo '<p>';
			echo '<label>File (.swf) </label>';
			echo '<input type="file" name="file" class="subtitle">';
		echo '</p>';
		echo '<br /> <br />';
		echo '<input name="id" type="hidden" value="'.$data['id_materi'].'" />';
		echo '<input name="path" type="hidden" value="'.$data['path'].'" />';
		echo '<div id="btn">';
		echo '<input type="submit" class="submit" name="edit" value="Simpan">';
		echo '<input class="submit" name="Batal" type="reset" value="Back" onclick="window.history.go(-1)"/>';
		echo '</form>';
		echo '</div>';
	echo '</div>';
}

elseif ($_GET['act'] == 'delete')
{
	$id = $_GET['id'];
	$sql = mysql_query("SELECT * FROM tb_materi WHERE id_materi = $id");
	$data=mysql_fetch_array($sql);
	unlink('../flash/'.$data['path']);
	
	mysql_query("DELETE from tb_materi where id_materi = $id");
	echo "<meta http-equiv=\"refresh\" content=\"0;URL=olah_materi.php?act=list\">";
	
	
}

?>




   
</div>
<!-- end main -->

<!-- footer -->
<?php include 'footer.php';?>
